package com.alibaba.aone.casestudy.module.action;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.Failure;
import com.alibaba.aone.casestudy.common.entity.FailureRpAsignGroup;
import com.alibaba.aone.casestudy.common.entity.FailureTime;
import com.alibaba.aone.casestudy.napoli.NapoliWorker;
import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.aone.casestudy.service.FailureTimeService;
import com.alibaba.aone.casestudy.service.GroupService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.util.StringUtil;
import com.alibaba.napoli.client.async.AsyncSender;

/**
 * 类FailureProcessAction.java的实现描述：TODO 类实现描述 
 * @author yongchun.chengyc 2011-5-1 上午08:21:21
 */
public class FailureProcessAction {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private FailureService failureService;
	@Autowired
	private GroupService groupService;
	@Autowired
	private FailureTimeService failureTimeService;
	
	@Autowired
	private AsyncSender sender;
	@Autowired
	private HttpServletResponse response;
	
	
	/*
	 * 这个类需要严重重构，需要ibatis的事务以及批处理。
	 * 由于要回学校写毕业论文，所以只是现将功能实现了。
	 * 
	 * 
	 * */
	
	public void doEdit(Context context) throws ParseException{
		String title = request.getParameter("title");
		String number = request.getParameter("number");
		String defini = request.getParameter("defini");
		String registrant = request.getParameter("registrant");
		Integer discoveryMethod = request.getParameter("discoveryMethod")==null ? 0:Integer.parseInt(request.getParameter("discoveryMethod"));
		String isReasonClear = request.getParameter("isReasonClear");
		Double score = Double.valueOf(request.getParameter("score"));
		String correlationDepartment = request.getParameter("correlationDepartment");
		String description = request.getParameter("description");
		String processingRecord = request.getParameter("processingRecord");

		Failure failure = new Failure();
		failure.setTitle(title);
		failure.setNumber(number);
		failure.setDefini(defini);
		failure.setRegistrant(registrant);
		failure.setDiscoveryMethod(discoveryMethod);
		failure.setIsReasonClear(isReasonClear);
		failure.setScore(score);
		failure.setCorrelationDepa(correlationDepartment);
		failure.setDescription(description);
		failure.setProcessingRecord(processingRecord);
		
		
		/*<==============================================================================================>*/
		String rpDepartment = request.getParameter("rpDepartment");
		String rpProductLine = request.getParameter("rpProductLine");
		String rpMan = request.getParameter("rpMan");
		String rpRole = request.getParameter("rpRole");
		String rpManager = request.getParameter("rpManager");
		Double rpAccount = request.getParameter("rpAccount")==null ? 0.0f:Double.valueOf(request.getParameter("rpAccount"));
		
		FailureRpAsignGroup group = new FailureRpAsignGroup();
		
		group.setFailureNumber(number);
		group.setRpRole(rpRole);
		group.setRpProductLine(rpProductLine);
		group.setRpManager(rpManager);
		group.setRpMan(rpMan);
		group.setRpAccount(rpAccount);
		group.setRpDepartment(rpDepartment);
		
		
		
		/*<==============================================================================================>*/
		String gmtStart1 = request.getParameter("gmtStart");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		
		Date gmtStart = StringUtil.isEmpty(gmtStart1)?null:format.parse(gmtStart1);
		
		String gmtEnd1 = request.getParameter("gmtEnd");
		Date gmtEnd =  StringUtil.isEmpty(gmtEnd1)?null:format.parse(gmtEnd1);
		
		int rank = request.getParameter("rank")==null ? 0:Integer.parseInt(request.getParameter("rank"));

		FailureTime failureTime = new FailureTime();
		failureTime.setFailureNumber(number);
		
		if(gmtStart!=null){
			failureTime.setGmtStart(gmtStart);
		}
		if(gmtEnd!=null){
			failureTime.setGmtEnd(gmtEnd);
		}
		failureTime.setRank(rank);
		
		

		
		groupService.update(group);
		
		failureTimeService.update(failureTime);
		
	}
	
	
	public void doAdd(Context context) throws ParseException, IOException{
	
		String title = request.getParameter("title");
		String number = request.getParameter("number");
		String defini = request.getParameter("defini");
		String registrant = request.getParameter("registrant");
		Integer discoveryMethod = Integer.parseInt(request.getParameter("discoveryMethod"));
		String isReasonClear = request.getParameter("isReasonClear");
		
		Double score = null==request.getParameter("score") ? 0.0:Double.valueOf(request.getParameter("score"));
		String correlationDepartment = request.getParameter("correlationDepartment");
		String description = request.getParameter("description");
		String processingRecord = request.getParameter("processingRecord");
		
		
		String rpDepartment = request.getParameter("rpDepartment");
		String rpProductLine = request.getParameter("rpProductLine");
		String rpMan = request.getParameter("rpMan");
		String rpRole = request.getParameter("rpRole");
		String rpManager = request.getParameter("rpManager");
		String rpAccount = request.getParameter("rpAccount");
		
		
		String gmtStart1 = request.getParameter("gmtStart");
		String gmtEnd1 = request.getParameter("gmtEnd");
		String rank = request.getParameter("rank");
		
		HashMap failureMap = new HashMap();
		failureMap.put("isFailureMsg", Boolean.TRUE);
		failureMap.put("title", title);
		failureMap.put("number", number);
		failureMap.put("defini", defini);
		failureMap.put("registrant", registrant);
		failureMap.put("discoveryMethod", discoveryMethod);
		failureMap.put("isReasonClear", isReasonClear);
		failureMap.put("score", score);
		failureMap.put("correlationDepartment", correlationDepartment);
		failureMap.put("description", description);
		failureMap.put("processingRecord", processingRecord);
		
		
		failureMap.put("rpDepartment", rpDepartment);
		failureMap.put("rpProductLine", rpProductLine);
		failureMap.put("rpMan", rpMan);
		failureMap.put("rpRole", rpRole);
		failureMap.put("rpManager", rpManager);
		failureMap.put("rpAccount", rpAccount);
		
		
		failureMap.put("gmtStart1", gmtStart1);
		failureMap.put("gmtEnd1", gmtEnd1);
		failureMap.put("rank", rank);
		
		
		System.out.println("<------------------------------------------------------------------------------------->");
		System.out.println(processingRecord);
		System.out.println(description);
		System.out.println("<------------------------------------------------------------------------------------->");
		 sender.send(failureMap);
		
		
		
		response.sendRedirect("failureDetailView.htm?number="+number);
		
		
	
	}
	
	
	public void doDelete(Context context) throws ParseException{
		String number = request.getParameter("number");
		number = "F55555555";
		failureService.delete(number);
		
	}
	

}